package com.hui.server.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @BelongsProject: redis
 * @BelongsPackage: com.example.demo.util
 * @Author: HUI
 * @CreateTime: 2022-03-18 16:52
 * @Description: 用来获取 springboot 创建好的工厂，spring 分工厂
 */

@Component
public class ApplicationContextUtils implements ApplicationContextAware {

    //保留下来工厂
    private static ApplicationContext applicationContext;

    //将创建好工厂以参数形式传递给这个类
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextUtils.applicationContext = applicationContext;
    }


    //提供在工厂中获取对象的方法 //RedisTemplate  redisTemplate
    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

}
